package com.ostrich.demomvvm.ui.home.adapter

import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.ostrich.demomvvm.R
import com.ostrich.demomvvm.databinding.ItemVideoBinding
import com.ostrich.demomvvm.ui.home.entity.VideoEntity

/**
 * @Author : yaotianxue
 * @Time : On 2023/10/19 09:24
 * @Description : VideoAdapter
 */
class VideoAdapter:RecyclerView.Adapter<VideoAdapter.VideoViewHolder>() {
    private val data:MutableList<VideoEntity> = mutableListOf()

    /**
     * 设置新的数据源
     */
     fun setData(data:MutableList<VideoEntity>){
        this.data.clear()
        this.data.addAll(data)
        notifyDataSetChanged()
    }

    /**
     * 追加数据源
     */
     fun addData(data:MutableList<VideoEntity>){
        val index = this.data.size
        this.data.addAll(data)
        // 范围刷新，从下标index刷新多少个
        notifyItemRangeChanged(index,data.size)
    }



    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VideoViewHolder {
        val itemVideoBinding =  DataBindingUtil.inflate<ItemVideoBinding>(LayoutInflater.from(parent.context), R.layout.item_video,parent,false)
        return VideoViewHolder(itemVideoBinding)
    }

    override fun onBindViewHolder(holder: VideoViewHolder, position: Int) {

        holder.itemVideoBinding.video11 =data[position]


    }

    override fun getItemCount(): Int {
        return data.size
    }

    class VideoViewHolder(var itemVideoBinding: ItemVideoBinding):RecyclerView.ViewHolder(itemVideoBinding.root){

    }
}